package com.zxy.supplier_system.server.utils.wechat

import com.zxy.supplier_system.server.utils.LoggerDelegate
import com.zxy.supplier_system.server.utils.wechat.official.WechatOfficialAccountHelper
import org.springframework.http.ResponseEntity


object ErrorCodeAndMessageResponseErrorHandler{
    internal val log by LoggerDelegate()
}

fun <T> WechatOfficialAccountHelper.validate200ErrorCodeAndMessageResponse(entity: ResponseEntity<Map<String, Any>>, javaClass: Class<T>):T {
    val body = entity.body
    ErrorCodeAndMessageResponseErrorHandler.log.info("validate 200 body {}",body)
    if (body!= null){
        val errcode = body["errcode"]
        if ( errcode != null && errcode != 0){
            val errmsg = body["errmsg"] as String?
            throw WechatApiException(errcode as Int, errmsg)
        }
    }
    return objectMapper.convertValue(body,javaClass)
}

fun WechatOfficialAccountHelper.validate200WechatApiErrorResponse(wechatApiErrorResponse: WechatApiErrorResponse): Unit {
    if ( wechatApiErrorResponse.errcode!=0){
        val errmsg = wechatApiErrorResponse.errmsg
        throw WechatApiException(wechatApiErrorResponse.errcode, errmsg)
    }
}